﻿using System.Windows;

namespace Bookvar.UI
{
    internal static class SizeHelper
    {
        //public static Rect GetCenteredRect(Size elementSize, Point center)
        //{
        //    double x = center.X - elementSize.Width / 2;
        //    double y = center.Y - elementSize.Height / 2;

        //    return new Rect( x, y, elementSize.Width, elementSize.Height );
        //}

        public static Rect GetMapRectangle( MindMapPanel panel )
        {
            Point topLeft = new Point( panel.Width, panel.Height );
            Point bottomRight = new Point( 0, 0 );
            GetTopicTopLeftRecursive( panel.MainTopicPresenter, ref topLeft );
            GetTopicBottomRightRecursive( panel.MainTopicPresenter, ref bottomRight );
            return new Rect( topLeft, bottomRight );
        }

        private static void GetTopicTopLeftRecursive( TopicContentPresenter topicContentPresenter, ref Point topLeft )
        {
            if ( topLeft.X > topicContentPresenter.X )
            {
                topLeft.X = topicContentPresenter.X;
            }
            if ( topLeft.Y > topicContentPresenter.Y )
            {
                topLeft.Y = topicContentPresenter.Y;
            }
            foreach ( TopicContentPresenter child in topicContentPresenter.Children )
            {
                GetTopicTopLeftRecursive( child, ref topLeft );
            }
        }

        private static void GetTopicBottomRightRecursive( TopicContentPresenter topicContentPresenter,
                                                          ref Point bottomRight )
        {
            if ( bottomRight.X < ( topicContentPresenter.X + topicContentPresenter.ActualWidth ) )
            {
                bottomRight.X = ( topicContentPresenter.X + topicContentPresenter.ActualWidth );
            }
            if ( bottomRight.Y < ( topicContentPresenter.Y + topicContentPresenter.ActualHeight ) )
            {
                bottomRight.Y = topicContentPresenter.Y + topicContentPresenter.ActualHeight;
            }
            foreach ( TopicContentPresenter child in topicContentPresenter.Children )
            {
                GetTopicBottomRightRecursive( child, ref bottomRight );
            }
        }
    }
}